<?php
/*
 *      pmanager.class.php
 *      
 *      Copyright 2009 Valery Tverdohleb <tvsdesign@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

class PManager
{
	public function getOnlineCount()
	{
		$stmt = Engine::get()->db->prepare('SELECT COUNT(*) FROM `users` WHERE (`last_activity` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE) AND `logged`);');
		$stmt->execute();
		return $stmt->fetchColumn();
	}
	
	public function isOnline($id)
	{
		$stmt = Engine::get()->db->prepare('SELECT (`last_activity` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE) AND `logged`) as `online` FROM `users` WHERE `id` = :id LIMIT 1;');
		$stmt->bindValue(':id', $id);
		$stmt->execute();
		return $stmt->fetchColumn();
	}
	
	public function isPlayerAtPos($coords, $id)
	{
		if ($coords['L'] == 0)
		{
			$stmt = Engine::get()->db->prepare('SELECT COUNT(*) FROM `users` WHERE `X` = :X AND `Y` = :Y  AND `Z` = :Z AND `L` = 0 AND `last_activity` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE) AND `logged` AND `id` = :id;');
			$stmt->bindValue(':X',$coords['X']);
			$stmt->bindValue(':Y',$coords['Y']);
			$stmt->bindValue(':Z',$coords['Z']);
		}
		else
		{
			$stmt = Engine::get()->db->prepare('SELECT COUNT(*) FROM `users` WHERE `L` = :L AND `last_activity` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE) AND `logged` AND `id` = :id;');
			$stmt->bindValue(':L',$coords['L']);
			
		}
		$stmt->bindValue(':id',$id);
		$stmt->execute();
		return ($stmt->fetchColumn() == 1);
	}
	
	/*
	 * Получение списка игроков, находящихся в одной точке пространства
	 */
	
	public function listPlayersAtPos($coords)
	{
		if ($coords['L'] == 0)
		{
			$stmt = Engine::get()->db->prepare('SELECT `id`, `name` FROM `users` WHERE `X` = :X AND `Y` = :Y  AND `Z` = :Z AND `L` = 0 AND `last_activity` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE) AND `logged`;');
			$stmt->bindValue(':X',$coords['X']);
			$stmt->bindValue(':Y',$coords['Y']);
			$stmt->bindValue(':Z',$coords['Z']);
		}
		else
		{
			$stmt = Engine::get()->db->prepare('SELECT `id`, `name` FROM `users` WHERE `L` = :L AND `last_activity` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE) AND `logged`;');
			$stmt->bindValue(':L',$coords['L']);
		}
		$stmt->execute();
		$r = array();
		while ($i = $stmt->fetch(PDO::FETCH_ASSOC))
		{
			$r[$i['id']] = $i;
		}
		return $r;
	}
	
	public function getPlayerDetails($id)
	{
		$stmt = Engine::get()->db->prepare('SELECT `id`, `name`, (`last_activity` >= DATE_SUB(NOW(), INTERVAL 5 MINUTE) AND `logged`) as `online` FROM `users` WHERE `id` = :id LIMIT 1;');
		$stmt->bindValue(':id', $id);
		$stmt->execute();
		return $stmt->fetch(PDO::FETCH_ASSOC);
	}
	
}

?>
